<template>
  <div class="app-container">
    <template v-if="showCoinsPage == true">
      <div class="search-bar">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item label="状态">
            <enum-select v-model="queryParams.status" placeholder="请选择状态" :enums="CASH_TYPE_ENUM"
              clearable></enum-select>
          </el-form-item>
          <el-form-item label="申请时间">
            <el-date-picker v-model="tempTime" :editable="false" type="datetimerange" range-separator="~"
              start-placeholder="开始时间" end-placeholder="截止时间" value-format="YYYY-MM-DD HH:mm:ss"
              @change="handleChange" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">
              <template #icon>
                <Search />
              </template>
              搜索
            </el-button>
            <el-button @click="handleResetQuery">
              <template #icon>
                <Refresh />
              </template>
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <BasePageTable :table-data="tableData" :table-columns-data="tableColumnsData" :total="total" :loading="loading"
        @handleQuery="changePage">
        <template #index="scope">
          {{ scope.index + 1 }}
        </template>
        <template #coinsNumber="{ row }">
          {{ row.coins / 10 }}
        </template>
        <template #status="{ row, name }">
          <el-tag :type="statusTag[row.status]">
            {{ getLabelByValue(CASH_TYPE_ENUM, row.status) }}
          </el-tag>
        </template>
        <template #operation="{ row, name }">
          <el-button link type="primary" :disabled="row.status != 0" @click="handleEdit(row, 'handle')">
            处理
          </el-button>
          <el-button link type="primary" :disabled="row.status != 1" @click="accountsHandle(row)">
            转账
          </el-button>
          <el-button link type="primary" @click="handleEdit(row, 'look')">查看详情</el-button>
        </template>
      </BasePageTable>
      <el-dialog v-model="dialogVisible" title="财务转账" width="500" :before-close="handleClose">
        <div>
          <el-form ref="formPut" :rules="rules" :model="caslAuto" label-width="auto">
            <el-form-item label="转账时间" prop="transferTime">
              <el-date-picker v-model="caslAuto.transferTime" type="datetime" format="YYYY/MM/DD hh:mm:ss"
                value-format="YYYY-MM-DD hh:mm:ss" style="width: 400px" />
            </el-form-item>
            <el-form-item label="上传凭证" prop="transferFile">
              <MultiImageUpload v-model="caslAuto.transferFile" :showTip="false" :limit="5" />
            </el-form-item>
          </el-form>
        </div>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="handleClose">取消</el-button>
            <el-button type="primary" @click="sbumit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </template>
    <template v-else>
      <coninsCashForm @backTo="cashBackTo" :type="cashType" :orderData="orderData"></coninsCashForm>
    </template>
  </div>
</template>
<script setup>
import { CASH_TYPE_ENUM } from "@/enums/conins/coinsCash";
import coninsCashForm from "../components/conisCashForm/index.vue";
import cashApi from "@/api/coins/cash-api";
import { getLabelByValue, getLabelValueList } from "@/utils/enums";
const { proxy } = getCurrentInstance();

const showCoinsPage = ref(true);
const loading = ref(false);
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  status: "",
  endTime: "",
  beginTime: "",
});
const statusTag = ref({
  0: "info",
  1: "success",
  2: "warning",
  3: "success",
});
const tempTime = ref([]);
const tableData = ref([]);
const dialogVisible = ref(false);
const orderData = ref({});
const total = ref(0);
const cashType = ref("");
const caslAuto = ref({
  id: "",
  transferTime: "",
  transferFile: [],
});
const tableColumnsData = ref([
  {
    label: "序号",
    prop: "index",
    align: "center",
    width: 60,
    customSlot: "index",
  },
  {
    prop: "viewUserVo.nickname",
    label: "申请人员",
    showOverflowTooltip: true,
  },
  {
    prop: "companyName",
    label: "公司名称",
    showOverflowTooltip: true,
  },
  {
    prop: "currentCoins",
    label: "总积分",
    showOverflowTooltip: true,
  },
  {
    prop: "coins",
    label: "提现积分",
    showOverflowTooltip: true,
  },
  {
    prop: "coinsNumber",
    label: "提现金额(元)",
    showOverflowTooltip: true,
    customSlot: "coinsNumber",
  },
  // {
  //   prop: "categoryText",
  //   label: "剩余积分",
  //   showOverflowTooltip: true,
  // },
  // {
  //   prop: "receiptFile",
  //   label: "发票凭证",
  //   showOverflowTooltip: true,
  // },
  {
    prop: "status",
    label: "状态",
    showOverflowTooltip: true,
    customSlot: "status",
  },
  {
    prop: "applyTime",
    label: "申请时间",
    showOverflowTooltip: true,
  },
  {
    prop: "operation",
    label: "操作",
    width: 150,
    align: "center",
    fixed: "right",
    customSlot: "operation", // 自定义插槽名称
  },
]);
const rules = ref({
  operationStartTime: [{ required: true, message: "请确认转账时间", trigger: "change" }],
  transferFile: [{ required: true, message: "请上传凭证", trigger: "change" }],
});

function handleSearch() {
  queryParams.pageNum = 1;
  getList();
}
// 重置查询
function handleResetQuery() {
  // queryParams.value.resetFields();
  queryParams.pageNum = 1;
  queryParams.status = "";
  queryParams.beginTime = "";
  queryParams.endTime = "";
  tempTime.value = [];
  getList();
}

function changePage(val) {
  queryParams.pageNum = val.pageNum;
  queryParams.pageSize = val.pageSize;
  getList();
}

function handleChange(val) {
  if (val) {
    const [startTime, endTime] = val;
    queryParams.beginTime = startTime || "";
    queryParams.endTime = endTime || "";
  } else {
    queryParams.beginTime = "";
    queryParams.endTime = "";
  }
}

function getList() {
  try {
    loading.value = true;
    cashApi
      .getPage(queryParams)
      .then((data) => {
        tableData.value = data.list || [];
        total.value = data.total;
      })
      .finally(() => {
        loading.value = false;
      });
  } catch (err) { }
}

function accountsHandle(row) {
  caslAuto.value.id = row.id;
  dialogVisible.value = true;
}

function sbumit() {
  console.log(caslAuto.value);

  proxy.$refs["formPut"].validate(async (valid) => {
    if (valid) {
      loading.value = true;
      let data = JSON.parse(JSON.stringify(caslAuto.value));
      data.transferFile = JSON.stringify(data.transferFile);
      try {
        cashApi
          .accounts(data)
          .then((res) => {
            getList();
            handleClose();
          })
          .finally(() => {
            loading.value = false;
          });
      } catch { }
    }
  });
}

// 关闭上传
function handleClose() {
  dialogVisible.value = false;
  caslAuto.value.id = "";
  caslAuto.value.transferFile = [];
  caslAuto.value.transferTime = "";
}

function handleEdit(row, type) {
  cashType.value = type;
  orderData.value = row;
  orderData.value.receiptFiles = JSON.parse(orderData.value.receiptFile);
  orderData.value.coinsNumber = orderData.value.coins / 10;
  showCoinsPage.value = false;
}

function cashBackTo() {
  showCoinsPage.value = true;
  getList();
}

onMounted(async () => {
  getList();
});
</script>
